package cn.edu.huanu.controller;

import cn.edu.huanu.service.IndexService;
import cn.edu.huanu.service.impl.IndexServiceImpl;
import cn.edu.huanu.utils.JsonUtils;
import cn.edu.huanu.utils.R;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author hb
 * @date 2025-11-06 15:39:21
 */
@WebServlet("*.index")
public class IndexManagerAction extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String servletPath = req.getServletPath();
        IndexService indexService=new IndexServiceImpl();

        PrintWriter out = resp.getWriter();
        if("/banner.index".equals(servletPath)){
            R r = indexService.loadBanner();
            out.println(JsonUtils.objectToJson(r));
        }else if("/category.index".equals(servletPath)){
            String parentIdParmas = req.getParameter("parentId");
            Integer parentId = parentIdParmas == null ? null:Integer.parseInt(parentIdParmas);
            R r = indexService.loadParentCateogry(parentId);
            out.println(JsonUtils.objectToJson(r));
        }else if("/books.index".equals(servletPath)){
            R r = indexService.loadIndexBooks();
            out.println(JsonUtils.objectToJson(r));
        }
    }
}
